package top.healthylife.service.impl;/*
 *@Author GaoZeXi
 *@Created time 2020/4/10 16:15
 *@Description:
 * Step by Step  and Stand on,  You Are The Best Investment!!!
 */

import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import top.healthylife.domain.OrderSetting;
import top.healthylife.mapper.OrderSettingMapper;
import top.healthylife.service.OrderSettingService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public void add(List<OrderSetting> orderSettings) {
        //根据orderSetting的日期来判断是执行更新还是插入操作
        if (orderSettings != null && orderSettings.size() > 0) {
            for (OrderSetting orderSetting : orderSettings) {
                boolean flag = orderSettingMapper.isExistedOrderDate(orderSetting.getOrderDate()) > 0 ? true : false;
                //执行更新
                if (flag) {
                    //根据预约日期更新预约人数;
                    orderSettingMapper.updateNumber(orderSetting);
                } else {
                    //执行添加操作
                    orderSettingMapper.insertNumber(orderSetting);
                }
            }
        }

    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {//2019-03
        List<OrderSetting> orderSettings = orderSettingMapper.getOrderSettingByMonth(date);
        List<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettings) {
            Map<String, Object> orderSettingMap = new HashMap<>(16);
            //Fixme 使用 Calendar.get(Calendar.DAY_OF_MONTH)来替换
            orderSettingMap.put("date", orderSetting.getOrderDate().getDate());
            orderSettingMap.put("number", orderSetting.getNumber());
            orderSettingMap.put("reservation", orderSetting.getReservations());
            data.add(orderSettingMap);
        }
        return data;
    }

    @Override
    public void updateNumberByDate(OrderSetting orderSetting) {
        //根据预约日期修改单个日期的预约人数
        //根据orderSetting的日期来判断是执行更新还是插入操作
        boolean flag = orderSettingMapper.isExistedOrderDate(orderSetting.getOrderDate()) > 0 ? true : false;
        //执行更新
        if (flag) {
            //根据预约日期更新预约人数;
            orderSettingMapper.updateNumber(orderSetting);
        } else {
            //执行添加操作
            orderSettingMapper.insertNumber(orderSetting);
        }
    }
}
